﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Library;
using System.Data;

/*
-- =============================================
-- Author:		Duong Hoang Vinh
-- Create date: 9/11/2011
-- Description:	Category Class
-- =============================================
*/

namespace BusinessLayer
{
   public class Category
    {
    #region "Variables and Properties"
    //Variables
        private string category_ID;
        private string parent_Category_ID;
        private string categoroy_Name;                        
        private string detail;        
        private int status;        

    //Properties
        public string Category_ID
        {
            get { return category_ID; }
            set { category_ID = value; }
        }

        public string Parent_Category_ID
        {
            get { return parent_Category_ID; }
            set { parent_Category_ID = value; }
        }

        public string Categoroy_Name
        {
            get { return categoroy_Name; }
            set { categoroy_Name = value; }
        }

        public string Detail
        {
            get { return detail; }
            set { detail = value; }
        }

        public int Status
        {
            get { return status; }
            set { status = value; }
        }
    #endregion

    #region "Initial Methods"
        public Category() { }

        public Category(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                Category_ID = sdr.GetString(0);
                if (!sdr.IsDBNull(1))
                    Parent_Category_ID = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    Categoroy_Name = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    Detail = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    Status = sdr.GetInt32(4);
            }
        }
    #endregion

    #region "Bussiness Methods"
    //Get All Category
        public static List<Category> GetAll()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Category_SHOWALL";
            List<Category> lstCategory = new List<Category>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            while (sdr.Read())
            {
                lstCategory.Add(new Category(sdr));
            }

            return lstCategory;
        }

    //Get One Category
        public static Category GetOne(string idCategory)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Category_SHOWONE";
            Category catetory = new Category();
            SqlDataReader sdr;

            dbh.AddParameter("@Category_ID", idCategory);
            sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            if (sdr.Read())
            {
                catetory.Category_ID = sdr.GetString(0);
                if (!sdr.IsDBNull(1))
                    catetory.Parent_Category_ID = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    catetory.Categoroy_Name = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    catetory.Detail = sdr.GetString(3);
                if (@sdr.IsDBNull(4))
                    catetory.Status = sdr.GetInt32(4);
            }

            return catetory;
        }
    //Get Parent Category
        public static List<Category> GetParent()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Category_PARENTCATE";
            List<Category> lstCategory = new List<Category>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);

            while (sdr.Read())
            {
                lstCategory.Add(new Category(sdr));
            }

            return lstCategory;
        }
    //Insert Category
        public static void Insert(Category category)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Category_INSERT";
            dbh.AddParameter("@Category_ID", category.Category_ID);
            dbh.AddParameter("@Parent_Category_ID", category.Parent_Category_ID);
            dbh.AddParameter("@Category_Name", category.Categoroy_Name);
            dbh.AddParameter("@Detail", category.Detail);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Update Category
        public static void Update(Category category)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Category_UPDATE";

            dbh.AddParameter("@Category_ID", category.Category_ID);
            dbh.AddParameter("@Parent_Category_ID", category.Parent_Category_ID);
            dbh.AddParameter("@Category_Name", category.Categoroy_Name);
            dbh.AddParameter("@Detail", category.Detail);

            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

    //Delete Category
        public static void Delete(string idCategory)
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Category_DELETE";

            dbh.AddParameter("@Category_ID", idCategory);
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
    #endregion
    }
}
